package com.ruoyi.wms.service;

import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.wms.domain.entity.MailboxMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.domain.bo.MailboxStatisticsBo;
import com.ruoyi.wms.domain.vo.MailboxStatisticsVo;
import com.ruoyi.wms.domain.entity.MailboxStatistics;
import com.ruoyi.wms.mapper.MailboxStatisticsMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 数据统计Service业务层处理
 *
 * @author zcc
 * @date 2024-12-14
 */
@RequiredArgsConstructor
@Service
public class MailboxStatisticsService {

    private final MailboxStatisticsMapper mailboxStatisticsMapper;

    /**
     * 查询数据统计
     */
    public MailboxStatisticsVo queryById(Long id){
        return mailboxStatisticsMapper.selectVoById(id);
    }

    public MailboxStatisticsVo ToadyData(){
        return mailboxStatisticsMapper.ToadyData();
    }

    public MailboxStatisticsVo Newest(){
        return mailboxStatisticsMapper.Newest();
    }

    /**
     * 查询数据统计列表
     */
    public TableDataInfo<MailboxStatisticsVo> queryPageList(MailboxStatisticsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MailboxStatistics> lqw = buildQueryWrapper(bo);
        Page<MailboxStatisticsVo> result = mailboxStatisticsMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询数据统计列表
     */
    public List<MailboxStatisticsVo> queryList(MailboxStatisticsBo bo) {
        LambdaQueryWrapper<MailboxStatistics> lqw = buildQueryWrapper(bo);
        return mailboxStatisticsMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MailboxStatistics> buildQueryWrapper(MailboxStatisticsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MailboxStatistics> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTodayQuantity() != null, MailboxStatistics::getTodayQuantity, bo.getTodayQuantity());
        lqw.eq(bo.getWeekQuantity() != null, MailboxStatistics::getWeekQuantity, bo.getWeekQuantity());
        lqw.eq(bo.getMonthQuantity() != null, MailboxStatistics::getMonthQuantity, bo.getMonthQuantity());
        lqw.eq(bo.getTotal() != null, MailboxStatistics::getTotal, bo.getTotal());
        lqw.eq(bo.getProcessedTotal() != null, MailboxStatistics::getProcessedTotal, bo.getProcessedTotal());
        lqw.eq(bo.getPendingTotal() != null, MailboxStatistics::getPendingTotal, bo.getPendingTotal());
        lqw.eq(bo.getEfficiency() != null, MailboxStatistics::getEfficiency, bo.getEfficiency());
        lqw.eq(bo.getTodayProcessedTotal() != null, MailboxStatistics::getTodayProcessedTotal, bo.getTodayProcessedTotal());
        lqw.orderByDesc(MailboxStatistics::getCreateTime);
        return lqw;
    }

    /**
     * 新增数据统计
     */
    public void insertByBo(MailboxStatisticsBo bo) {
        MailboxStatistics add = MapstructUtils.convert(bo, MailboxStatistics.class);
        mailboxStatisticsMapper.insert(add);
    }

    /**
     * 修改数据统计
     */
    public void updateByBo(MailboxStatisticsBo bo) {
        MailboxStatistics update = MapstructUtils.convert(bo, MailboxStatistics.class);
        mailboxStatisticsMapper.updateById(update);
    }

    /**
     * 批量删除数据统计
     */
    public void deleteByIds(Collection<Long> ids) {
        mailboxStatisticsMapper.deleteBatchIds(ids);
    }
}
